package com.cqx.collectionMysql.util;

import java.util.List;

public class PageUtils<T>{

    private int start; // 启始条数
    private int end; // 结束条数
    private int totalPage; // 总页数
    private int pageNum; // 当前页
    private long totalRow; // 总条数
    private int pageSize; // 分页单位
    private List<T> list;  //当前页的数据

    public int getPageForPageable(int pageNum){
        return pageNum - 1;
    }

    public void setPaginationList(Long count,List<T> list,int pageNum,int pageSize){
        this.list = list;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.setTotalRow(count);
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }



    public int getStart() {
        return start;
    }

    public int getEnd() {
        return end;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getPageNum() {
        return pageNum;
    }

    public long getTotalRow() {
        return totalRow;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public void setTotalRow(long totalRow) {
        this.totalRow = totalRow;
        this.totalPage = (int) (totalRow / pageSize);
        if (totalRow % pageSize > 0)
            this.totalPage = this.totalPage + 1;



        this.pageNum = (pageNum <= 0) ? 1 : pageNum;


        if (this.pageNum > this.totalPage)
            this.pageNum = this.totalPage;
        if(totalPage==0){
            this.pageNum=1;
        }
        this.start = (this.pageNum - 1) * pageSize;
        this.end = this.start + pageSize-1;


    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public PageUtils() {
        super();
    }
}
